import io
import sys
import mysql.connector

# 改变标准输出的默认编码
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf8')

import requests
from bs4 import BeautifulSoup

# MySQL 数据库连接配置
config = {
    'user': 'root',
    'password': '123456',
    'host': 'localhost',
    'database': 'bookshare',
    'raise_on_warnings': True
}

# 发送 GET 请求获取网页内容
url = 'https://www.readnovel.com/category/30055_f1_f1_f1_f1_f1_0_20'

response = requests.get(url)

# 解析网页内容
soup = BeautifulSoup(response.text, 'html.parser')

# 提取书籍名称和作者名字
book_infos = soup.find_all('div', class_='book-info')  # 一堆书籍信息

# 连接 MySQL 数据库
cnx = mysql.connector.connect(**config)
cursor = cnx.cursor()

for info in book_infos:
    link = info.h3.a['href']
    book_id = link.split('/')[2]
    book_author = info.find('h4').text.strip()
    book_name = info.find('h3').text.strip()
    book_msg = info.find('p', class_='intro').text.strip()
    book_link = 'https://www.readnovel.com/book/' + book_id

    # 准备 SQL 语句
    query = "INSERT INTO book (BookId, Title, Author, Type, Link) VALUES (%s, %s, %s, %s, %s)"
    values = (book_id, book_name, book_author, book_msg, book_link)

    # 执行 SQL 语句
    cursor.execute(query, values)

# 提交更改并关闭连接
cnx.commit()
cursor.close()
cnx.close()